home *** CD-ROM | disk | FTP | other *** search
- Path: gryphon.phoenix.net!usenet
- From: brucew@phoenix.net (Bruce Wedding)
- Newsgroups: comp.lang.c
- Subject: Re: Finding a prime number
- Date: Sun, 28 Jan 1996 18:49:55 GMT
- Organization: BranPaul Systems
- Message-ID: <4ege88$8mi@gryphon.phoenix.net>
- References: <4e875s$nqk@reader2.ix.netcom.com> <4e8pds$4va@nw002.infi.net>
- NNTP-Posting-Host: dial77.phoenix.net
- X-Newsreader: Moe's Newsreader
-
- nngis@norfolk.infi.net (Greg DiGiorgio) wrote:
-
-
- >#include <stdio.h>
-
- >int is_prime(int n) { /* Is it a prime number... */
-
- > int i; /* Loop counter */
- > for (i=2; i<n; i++) {
- You are wasting a lot of time here. There are two easy things
- you can do to speed this up. 1. Increment i by two so as to
- only check odd numbers. You will of course, have to make
- some other changes for this to work. 2. Only check up
- to the sqrt(n). Anything past that is redundant. Calculate
- the square root first, do not place it in the loop.
-
-
- Bruce D. Wedding Have Compiler, Will Travel!
- Perspicacious Programming Performed Promptly
- Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
-
-